package com.seeyon.apps.kksso.common;

import com.seeyon.ctp.util.UUIDLong;
import org.apache.commons.lang.StringUtils;

import java.nio.charset.StandardCharsets;
import java.util.Base64;

public class UserCoderUtil {
    public static String encrypt(String loginName){
        if (StringUtils.isBlank(loginName)){
            return null;
        }
        loginName = loginName + "@@@" + UUIDLong.longUUID();
        Base64.Encoder encoder = Base64.getEncoder();
        byte[] bytes = loginName.getBytes(StandardCharsets.UTF_8);
        loginName = encoder.encodeToString(bytes);
        return AESKit.encrypt(loginName);
    }

    public static String decrypt(String encryptStr){
        if (StringUtils.isBlank(encryptStr)){
            return null;
        }
        String base64Code = AESKit.decrypt(encryptStr);
        Base64.Decoder decoder = Base64.getDecoder();
        byte[] decode = decoder.decode(base64Code);
        String decodeStr = new String(decode, StandardCharsets.UTF_8);
        String[] split = decodeStr.split("@@@");
        if (split.length != 2) return null;
        return split[0];
    }
}
